﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using ShopOnline.Models;

namespace ShopOnline.Controllers
{
    public class PictureCatelogyController : Controller
    {
        private DAL_PictureCatelogy picCatelogyRepository;
        private ShopOnline.Models.ShopOnlineDBDataContext db = new Models.ShopOnlineDBDataContext();
        public PictureCatelogyController()
        {
            picCatelogyRepository = new DAL_PictureCatelogy();
            db = new Models.ShopOnlineDBDataContext();
        }
        //
        // GET: /PictureCatelogy/
         [Authorize]
        public ActionResult AdminListPictureCatelogy()
        {
            var list = picCatelogyRepository.getListPictureCatelogy();
            return View(list);
        }

        //
        // GET: /PictureCatelogy/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            var picCate = picCatelogyRepository.getPictureCatelogyById(id);
            return View(picCate);

        }
        //
        // GET: /PictureCatelogy/Create
         [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /PictureCatelogy/Create
         [Authorize]
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                PictureCatelogy picCate = new PictureCatelogy();
                picCate.curent = false;
                picCate.description=collection["description"];
                picCate.imagePath= collection["imagePath"];
                picCate.title = collection["title"];
                var show = collection["show"];
                if (show=="false")
                {
                    picCate.show = false;
                }
                else
                {
                    picCate.show = true;
                }
                db.PictureCatelogys_add(picCate.title, picCate.description, picCate.imagePath, picCate.show, picCate.curent);
                return RedirectToAction("AdminListPictureCatelogy");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /PictureCatelogy/Edit/5
  [Authorize]
        public ActionResult Edit(int id)
        {
            var picCate= picCatelogyRepository.getPictureCatelogyById(id);
            return View(picCate);
        }

        //
        // POST: /PictureCatelogy/Edit/5
         [Authorize]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var picCate = picCatelogyRepository.getPictureCatelogyById(id);
                picCate.curent = false;
                picCate.description = collection["description"];
                picCate.imagePath = collection["imagePath"];
                picCate.title = collection["title"];
                var show = collection["show"];
                if (show == "false")
                {
                    picCate.show = false;
                }
                else
                {
                    picCate.show = true;
                }
                db.PictureCatelogys_update(picCate.pictureCatelogyId,picCate.title, picCate.description, picCate.imagePath, picCate.show, picCate.curent);
                return RedirectToAction("AdminListPictureCatelogy");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /PictureCatelogy/Delete/5
  [Authorize]
        public ActionResult Delete(int id)
        {
            var picCate = picCatelogyRepository.getPictureCatelogyById(id);
            return View(picCate);
        }

        //
        // POST: /PictureCatelogy/Delete/5
 [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var picCate = picCatelogyRepository.getPictureCatelogyById(id);
                picCatelogyRepository.DeleteCatelogy(picCate);
                return RedirectToAction("AdminListPictureCatelogy");
            }
            catch
            {
                return View();
            }
        }
    }
}
